package com.runda.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.bbjob.dto.Result;
import com.bbjob.model.VersionUpgrade;
import com.runda.entity.JwtUser;
import com.runda.sec.SecContextUtil;
import com.runda.service.IVersionUpgradeService;
import com.runda.util.DataEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags="VersionUpgradeApi", description="升级管理")
@RestController
@RequestMapping(path="versionupgrade", produces="application/json;charset=utf-8")
public class VersionUpgradeController {

	@Resource
	private IVersionUpgradeService versionservice;
	
	@ApiOperation(value="新增授权码", notes="新增授权码")
	@ApiImplicitParams({
		@ApiImplicitParam(name="versionNo", value="版本号", required=true, dataType="String"),
		@ApiImplicitParam(name="type", value="安装包类型1出题端2考试端", required=true, dataType="Integer"),
		@ApiImplicitParam(name="remark", value="版本升级描述(带换行符的textarea等输入框)", required=true, dataType="String")
	})
	@RequestMapping(value="upload", method=RequestMethod.POST)
	public Result upload(HttpServletRequest request, @RequestParam(value = "file", required = true) MultipartFile multipartFile) {
		JwtUser user = SecContextUtil.getCurrentUser();
		int ok = versionservice.upload(request,user.getUsername(),multipartFile);
		switch (ok) {
		case 0:
			return new Result(8106,"建立信息失败!");
		case 1:
			return new Result(200, "新增成功!");
		case -1:
			return new Result(8106,"新增失败！");
		default:
			return new Result(8106,"未知错误！");
		}
	}
	
	@ApiOperation(value="查看升级(出题端)", notes="查看升级")
	@RequestMapping(value="checkUpgradeForQuestion", method=RequestMethod.GET)
	public DataEntity checkUpgradeForQuestion() {
		SecContextUtil.getCurrentUser();
		return DataEntity.getInstance(versionservice.checkUpgrade());
	}
	
	@ApiOperation(value="查看升级(考试端)", notes="查看升级")
	@RequestMapping(value="checkUpgradeForExam", method=RequestMethod.GET)
	public DataEntity checkUpgradeForExam() {
		SecContextUtil.getCurrentUser();
		return DataEntity.getInstance(versionservice.checkUpgradeForExam());
	}
	
	@ApiOperation(value="获取列表", notes="查看升级")
	@ApiImplicitParams({
		@ApiImplicitParam(name="versionNo", value="版本号", required=false, dataType="String"),
		@ApiImplicitParam(name="type", value="安装包类型1出题端2考试端", required=false, dataType="Integer"),
		@ApiImplicitParam(name="remark", value="版本升级描述", required=false, dataType="String")
	})
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getList(VersionUpgrade version, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		return DataEntity.getInstance(versionservice.getlist(version, page, rows));
	}
	
	@ApiOperation(value="删除升级", notes="删除升级")
	@RequestMapping(value="delete", method=RequestMethod.GET)
	public Result delete(Integer id) {
		SecContextUtil.getCurrentUser();
		int ok = versionservice.deleteUpgrade(id);
		switch (ok) {
		case 1:
			return new Result(200,"删除成功!");
		default:
			return new Result(8106,"删除失败！");
		}
	}
}
